package com.njdj.gps.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.njdj.gps.common.constants.NjdjConstants;
import com.njdj.gps.common.page.PageList;
import com.njdj.gps.common.page.PageProperty;
import com.njdj.gps.common.util.Utils;
import com.njdj.gps.kernel.entity.SeDevice;
import com.njdj.gps.kernel.entity.SeTask;
import com.njdj.gps.kernel.service.ISeDeviceService;
import com.njdj.gps.kernel.service.ISeTaskService;



@Controller
public class SeTaskController extends BaseController {
	
	@Autowired
	ISeTaskService seTaskService;
	@Autowired
	ISeDeviceService seDeviceService;
	
	@RequestMapping("/taskview.do")
	public ModelAndView loginHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws ServletException, DocumentException {
		
		//获取carids
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("status", 0);
		if(null != this.getRole(request) && "administrator".equals(this.getRole(request))){}
		else
			param.put("onwer", this.getUserName(request));
		List<SeDevice> deviceList = seDeviceService.listModel(param);
		model.put("deviceList", deviceList);
		
		String pageNo = getParameter(request, "pageNo");
		String pageSize = getParameter(request, "pageSize");
		int pageNum = 1;
		int pageSizeNum = NjdjConstants.PAGE_SIZE_DEFAULT;
		if (!"".equals(pageNo)) {
			pageNum = Utils.parseInt(pageNo, 1);
		}
		if (!"".equals(pageSize)) {
			pageSizeNum = Utils.parseInt(pageSize, 1);
		}
		PageProperty pp = new PageProperty();
		pp.setNpage(pageNum);
		pp.setNpagesize(pageSizeNum);
		
		
		String carid = this.getParameter(request, "carid");
		if(null != carid && !"".equals(carid)){
			pp.putParamMap("carid", carid);
			model.put("carid", carid);
		}
		
		String orderby = this.getParameter(request, "orderby");
		if(null != orderby && !"".equals(orderby)){
			pp.putParamMap("orderby", orderby);
			model.put("orderby", orderby);
		} else {
			pp.putParamMap("orderby", "QUERYTIME DESC");
		}
		
		String beginDate = this.getParameter(request, "beginDate");
		if(!"".equals(beginDate)){
			pp.putParamMap("beginDate", beginDate);
			model.put("beginDate", beginDate);
		}
		String endDate = this.getParameter(request, "endDate");
		if(!"".equals(endDate)) {
			pp.putParamMap("endDate", endDate);
			model.put("endDate", endDate);
		}
		
		PageList<SeTask> taskList = seTaskService.listSplitTaskView(pp);
		model.put("taskList", taskList);
		
			
		return new ModelAndView("task/taskview");
	}

}
